*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear

*******************************************************************************
* Rescale ideology and presidential vote
*******************************************************************************

su ideology
replace ideology = (ideology - r(min))/(r(max) - r(min))
su support
replace support = (support - r(min))/(r(max) - r(min))

*******************************************************************************
* For R3
*******************************************************************************

eststo clear
eststo: areg voteshare ideology cycle_* i.ncandidates if deputy==1, ///
	cl(district) a(state)
eststo: areg voteshare ideology cycle_* i.ncandidates if deputy==0, ///
	cl(district) a(state)
eststo: areg voteshare c.ideology##deputy cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner ideology cycle_* i.ncandidates if deputy==1, ///
	cl(district) a(state)
eststo: areg winner ideology cycle_* i.ncandidates if deputy==0, ///
	cl(district) a(state)
eststo: areg winner c.ideology##deputy cycle_* i.ncandidates, ///
	cl(district) a(state)
esttab, compress se keep(*ideology*)
	
*******************************************************************************
* Do the regressions with deputy, ideology, and incumbency
*******************************************************************************

eststo clear

eststo: areg voteshare deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg voteshare deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg voteshare deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

eststo: areg winner deputy cycle_* i.ncandidates, cl(district) a(state)
eststo: areg winner deputy ideology cycle_* i.ncandidates, ///
	cl(district) a(state)
eststo: areg winner deputy ideology incumbent cycle_* i.ncandidates, ///
	cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable ideology "Candidate Conservatism"

#delimit;

esttab
	using "_output/tableS4_a.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 0 1 0 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* Make interactions
*******************************************************************************

gen deputyXsupport = deputy*support
gen ideologyXsupport = ideology*support

*******************************************************************************
* Do the regressions with deputy and ideology
*******************************************************************************

eststo clear

eststo: areg voteshare ideology support ideologyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg voteshare deputy support deputyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)

eststo: areg winner ideology support ideologyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg winner deputy support deputyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable support "Constituency Conservatism"
label variable deputyXsupport "Experienced X Const. Conservatism"
label variable ideologyXsupport "Cand. Conservatism X Const. Conservatism"

#delimit;

esttab
	using "_output/tableS4_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		order(deputy ideology)
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 1 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
